package File;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Arrays;

/**
 * @ClassName Main
 * @Description TODO
 * @Author WSY
 * @Date 2020-6-5 12:31
 */

public class Main {
    public static void main(String[] args) {
        WindowDocument win = new WindowDocument();
        win.setBounds(100, 100, 590, 500);
        win.setTitle("排序单词");
    }
}

/**
 * @author WSY
 */
class WindowDocument extends JFrame {
    JTextArea inputText, showText;
    JMenuBar menubar;
    JMenu menu;
    JMenuItem itemCopy, itemCut, itemPaste, itemSave, itemOpen;
    TextListener textChangeListener;
    HandleListener handleListener;

    WindowDocument() {
        init();
        setLayout(new FlowLayout());
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    void init() {
        inputText = new JTextArea(16, 28);
        showText = new JTextArea(16, 28);
        showText.setLineWrap(true);
        showText.setWrapStyleWord(true);
        menubar = new JMenuBar();
        menu = new JMenu("编辑");
        itemCopy = new JMenuItem("复制(C)");
        itemCut = new JMenuItem("剪切(T)");
        itemPaste = new JMenuItem("粘贴(P)");
        itemSave = new JMenuItem("保存(S)");
        itemOpen = new JMenuItem("打开(O)");
        itemCopy.setAccelerator(KeyStroke.getKeyStroke('c'));
        itemCut.setAccelerator(KeyStroke.getKeyStroke('t'));
        itemPaste.setAccelerator(KeyStroke.getKeyStroke('p'));
        itemSave.setAccelerator(KeyStroke.getKeyStroke('s'));
        itemOpen.setAccelerator(KeyStroke.getKeyStroke('o'));
        itemCopy.setActionCommand("copy");
        itemCut.setActionCommand("cut");
        itemPaste.setActionCommand("paste");
        itemSave.setActionCommand("save");
        itemOpen.setActionCommand("open");
        menu.add(itemCopy);
        menu.add(itemCut);
        menu.add(itemPaste);
        menu.add(itemSave);
        menu.add(itemOpen);
        menubar.add(menu);
        setJMenuBar(menubar);
        add(new JScrollPane(inputText));
        add(new JScrollPane(showText));
        textChangeListener = new TextListener();
        handleListener = new HandleListener();
        textChangeListener.setInputText(inputText);
        textChangeListener.setShowText(showText);
        handleListener.setInputText(inputText);
        handleListener.setShowText(showText);
        (inputText.getDocument()).addDocumentListener(textChangeListener);
        itemCopy.addActionListener(handleListener);
        itemCut.addActionListener(handleListener);
        itemPaste.addActionListener(handleListener);
        itemOpen.addActionListener(handleListener);
        itemSave.addActionListener(handleListener);
    }
}

class HandleListener implements ActionListener {
    JTextArea inputText, showText;

    public void setInputText(JTextArea text) {
        inputText = text;
    }

    public void setShowText(JTextArea text) {
        showText = text;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JFileChooser jf = new JFileChooser();
        jf.setFileSelectionMode(JFileChooser.FILES_ONLY);
        String str = e.getActionCommand();
        if (str.equals("copy")) {
            showText.copy();
        } else if (str.equals("cut")) {
            showText.cut();
        } else if (str.equals("paste")) {
            inputText.paste();
        } else if (str.equals("open")) {
            jf.setDialogTitle("请选择要打开的文件");
            if (jf.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                String url = jf.getSelectedFile().getAbsolutePath();
                try (BufferedReader br = new BufferedReader(new FileReader(url))) {
                    while (true) {
                        String line = br.readLine();
                        if (line == null) {
                            break;
                        }
                        inputText.append(line + "\n");
                        showText.append(line + "\n");

                    }
                } catch (IOException a) {
                    a.printStackTrace();
                }
            }
        } else if (str.equals("save")) {
            jf.setDialogTitle("请选择要保存的文件");
            if (jf.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                String url = jf.getSelectedFile().getAbsolutePath();
                try(BufferedWriter bw = new BufferedWriter(new FileWriter(url))){
                    bw.write(inputText.getText());
                }catch (IOException a){
                    a.printStackTrace();
                }
            }
        }
    }
}

class TextListener implements DocumentListener {
    JTextArea inputText, showText;

    public void setInputText(JTextArea text) {
        inputText = text;
    }

    public void setShowText(JTextArea text) {
        showText = text;
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        String str = inputText.getText();
        String regex = "[\\s\\d\\p{Punct}]+";
        String[] words = str.split(regex);
        Arrays.sort(words);
        showText.setText(null);
        for (String word : words) {
            showText.append(word + ",");
        }
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        changedUpdate(e);
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        changedUpdate(e);
    }
}
